package com.xitang.common.service.impl;

import com.xitang.common.bean.BizMaterial;
import com.xitang.common.dto.MaterialQueryReq;
import com.xitang.common.dto.PageQueryRsp;
import com.xitang.common.mapper.BizMaterialMapper;
import com.xitang.common.service.IMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MaterialServiceImpl implements IMaterialService {
    @Autowired
    private BizMaterialMapper materialMapper;

    @Override
    public PageQueryRsp<BizMaterial> getMaterialList(MaterialQueryReq queryReq) {
        Long totalCount = materialMapper.selectCountByCustom(queryReq.getMaterialCode(), queryReq.getMaterialCode());
        if (Objects.isNull(totalCount) || totalCount < 1) {
            return new PageQueryRsp<>();
        }
        int pageNo = queryReq.getPageNo();
        int pageSize = queryReq.getPageSize();
        int offset = (pageNo - 1) * pageSize;
        List<BizMaterial> bizMaterials = materialMapper.selectDataByCustom(offset, pageSize, queryReq.getMaterialCode(), queryReq.getMaterialName());
        PageQueryRsp<BizMaterial> pageQueryRsp = new PageQueryRsp<>();
        pageQueryRsp.setRecords(bizMaterials);
        pageQueryRsp.setTotal(totalCount);
        return pageQueryRsp;
    }

    @Override
    public BizMaterial getMaterialDetail(String materialCode) {
        return materialMapper.selectById(materialCode);
    }

    @Override
    public void updateMaterialDetail(BizMaterial material) {
        materialMapper.updateById(material);
    }
}
